package filters;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import com.google.common.base.Throwables;
import com.google.common.collect.Lists;

import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.util.Span;


/**
 * Filter proper names for desired type; i.e., "person".<p>
 * 
 * Demonstration of Pipes and Filters Pattern: Filter.<p>
 * 
 * Use ApacheOpenNLP to determine sentence validity.<p>
 *
 * {@link http://opennlp.apache.org}
 */

public class NamesFilter
{
	ArrayList<String> _found = Lists.newArrayList();
	
	
	NamesFilter( String[] text, String nameType )
	{
		try( final InputStream modelIn = 
					new FileInputStream("en-ner-person.bin") ) 
		{
			final TokenNameFinderModel model = 
					new TokenNameFinderModel(modelIn);

			final NameFinderME nameFinder = new NameFinderME(model);

			Span nameSpans[] = nameFinder.find(text);
			
			for( Integer ct=0; ct < nameSpans.length; ct++ )
			{
				String typeFound = nameSpans[ct].getType();
			
				if( nameType.equalsIgnoreCase(typeFound) )
					_found.add( (text[ nameSpans[ct].getStart()] 
							+ " " 
							+ text[nameSpans[ct].getEnd()-1])
							.replace(",", "") );
			
			}//for
			
		}
		catch( final IOException err )
		{
			Throwables.propagate(err);
			
		}//try
		
	}//constructor
	

}//class
